package com.xyz.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import beans.User;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //创建需要用到的控件的变量
    private TextView tv1,tv2,tv3;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private TextView tvForUsername;
    Intent intent;
    User userAtPresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //处理一些逻辑关系
        doSomething();

        //绑定控件
        tv1=(TextView)findViewById(R.id.menu1);
        tv2=(TextView)findViewById(R.id.menu2);
        tv3=(TextView)findViewById(R.id.menu_welcome);
        //设置监听器，固定写法
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        //若是继承FragmentActivity，fm=getFragmentManger();
        fm=getSupportFragmentManager();
        //fm可以理解为Fragment显示的管理者，ft就是它的改变者
        ft=fm.beginTransaction();
        //默认情况下就显示FragmentWelcome
        ft.replace(R.id.content,new FragmentWelcome());
        //提交改变的内容
        ft.commit();
    }

    private void doSomething(){
        //实例化
        tvForUsername = (TextView) findViewById(R.id.tv_username);

        //获取来自LoginAtivity的intent
        intent = getIntent();
        userAtPresent = (User) intent.getSerializableExtra("aUser");

        if ( userAtPresent != null )
            tvForUsername.setText("享受你的音乐时光------"+userAtPresent.getUsername());
        else{
            tvForUsername.setText("由于您未登录账号，你将以”游客10086“的身份访问");
            userAtPresent = new User("none","游客10086","none",0);
        }

    }

    //创建选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //使用布局文件文件
        getMenuInflater().inflate(R.menu.option_menu,menu);

        return true;
    }

    //监听选项菜单
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case R.id.item_login:{
                //跳转到登录界面
                Intent intent = new Intent("servlet.loginWork");
                intent.setPackage("com.xyz.musicplayer");
                startActivity(intent);
            }break;
            case R.id.item_reload:{
                //重新扫描文件
                Toast.makeText(this, "扫描手机和电脑中的音频文件(to be done)", Toast.LENGTH_SHORT).show();
            }break;
            case R.id.item_userdetail:{
                //用户详情界面
                Intent intent = new Intent(MainActivity.this, UserDetailActivity.class);
                intent.putExtra("user",userAtPresent);
                startActivity(intent);
            }break;
            default:
                Toast.makeText(this, "通知管理员进行添加", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    @Override
    //控件的点击事件
    public void onClick(View v){

        ft=fm.beginTransaction();
        //切换选项卡
        switch (v.getId()){

            case R.id.menu1: {
                FragMusicList discoverFragment = new FragMusicList();
                ft.replace(R.id.content, discoverFragment);
                //传递参数到fragment
                Bundle bundle = new Bundle();
                bundle.putString("username", userAtPresent.getUsername());
                discoverFragment.setArguments(bundle);
            }break;
            case R.id.menu_welcome:{
                ft.replace(R.id.content,new FragmentWelcome());
            }break;
            case R.id.menu2: {
                ft.replace(R.id.content, new FragMusicListToBePlayed());
                Toast.makeText(this, "播放列表(to be done)", Toast.LENGTH_SHORT).show();
            }break;
            default:
                break;
        }
        ft.commit();
    }
}